/******************************************************************************
 *Copyright (C) 2024 EternalChip, Inc.(Gmbn) or its affiiates.
 *
 * All Rignts Reserved
 * 
 * @file system_adaption .h
 * 
 * @par dependencies
 * -bsp_led_driver.h
 * -bsp_led_handler.h
 * 
 * @author ssm |R&D Dept. |EternalChip 立芯嵌入式
 * 
 * biref Provide the APIs of LEDS and corresponding operations,
 * 
 * Processing flow;
 * 
 * call directly.
 * 
 * @version V1.0 2025-6-5
 * 
 * @note 1 tab == 4 spaces!
 * 
******************************************************************************/

#ifndef __SYSTEM_ADAPTION_H__
#define __SYSTEM_ADAPTION_H__

#define OS_SUPPIRTING               /*OS sUPPRTING depend on OS avalible      */
#define HANDLER_1_DEBUG

//******************************  Icludes ************************************//
//1.compiling system standard head file
#include <stdio.h>
#include <stdint.h>


//2.core lary head file 
#include "cmsis_os.h"
//2.1 driver 
//2.2 core
#include "main.h"
#include "usart.h"

//3.os layer head flie
#include "FreeRTOS.h"
#include "queue.h"
#include "task.h"

//4.bsp layer head file 
#include "bsp_led_driver.h"
#include "bsp_led_handler.h"

//******************************  Icludes ************************************//


//******************************  Defines ************************************//

#define INIY_PATTERN_SYSTEM (uint8_t )0xEC

typedef enum{
    SYSTEM_OK            =0,           /* LED Operation complete successfully    */
    SYSTEM_ERROR         =1,           /* LED Run-time error withoutcase matched */
    SYSTEM_ERRORTIMEROUT =2,           /* LED Operation falied with timeout      */
    SYSTEM_ERRORRESOURCE= 3,           /* LED Reasource not available            */
    SYSTEM_ERRORPARAMETER=4,           /* LED Parameter error                    */
    SYSTEM_ERRORNOMEMORY =5,           /* LED Out of memory                      */
    SYSTEM_ERRORISR      =6,           /* LED Not allowed in ISR context         */
    SYSTEM_RESERVED      =7,           /* LED Reserved                           */
} system_status_t;


//******************************  Defines ************************************//



//******************************  Declaring **********************************//






//*************************BAP Layer     ************************************//

//*****************BSP Layer Targets****************************//

__attribute__((used,section("bsp_targets")))
static bsp_led_handler_t 	 handler_1 ={INIY_PATTERN_SYSTEM}; 
__attribute__((used,section("bsp_targets")))
static bsp_led_driver_t         led1 ={INIY_PATTERN_SYSTEM};
__attribute__((used,section("bsp_targets")))
static bsp_led_driver_t         led2 ={INIY_PATTERN_SYSTEM};

//*****************BSP Layer Targets****************************//

//*****************BSP Layer adapters**************************//

led_status_t led_on_myown(void)
{
		printf("led is on\r\n");
		return LED_OK; 
}
led_status_t led_off_myown(void)
{
		printf("led is off\r\n");
		return LED_OK; 
}

led_operations_t led_operations_myown={
	.pf_led_on=led_on_myown,
	.pf_led_off=led_off_myown
};

led_status_t pf_get_time_mywon(uint32_t * const time_stamp)
{
		printf("get time now timmezore\r\n");
		*time_stamp=0;
		return LED_OK; 
}

time_base_ms_t time_base={
	.pf_get_time_ms=pf_get_time_mywon
};

led_status_t pf_os_delay_ms_mywon(const uint32_t *delay_time)
{
		printf("ps_os_delay_ms now delay 1ms\r\n");
		for(int i=0;i<500;i++);
		printf("delay [%d] ms,finsh\r\n",*delay_time);
		return LED_OK; 
}

os_delay_ms_t os_delay_mywon={
	.pf_os_delay_ms=pf_os_delay_ms_mywon
	
};


led_handler_status_t pf_os_delay_ms_handler_1(const uint32_t *delay_time)
{
#ifdef HANDLER_1_DEBUG
		printf("pf_os_delay_ms_handler_1\r\n");
#endif
		// for(int i=0;i<500;i++);
		// printf("delay [%d] ms,finsh\r\n",*delay_time);
		return HANDLER_OK; 
}

handler_os_delay_ms_t os_delay_handler={
	.pf_os_delay_ms=pf_os_delay_ms_handler_1,
	
};

led_handler_status_t pf_os_queue_create_handelr_1(
                          uint32_t const item_num,
                          uint32_t const item_size,
                          void  **const queue_handler
                          )
{
#ifdef HANDLER_1_DEBUG
		printf("pf_os_queue_create_handelr_1\r\n");
#endif
    QueueHandle_t temp_queue_handler=NULL;
     temp_queue_handler = xQueueCreate(item_num,item_size);
		if(NULL==*queue_handler)
		{
				return HANDLER_ERRORRESOURCE;
		}
		else
		{
				*queue_handler=temp_queue_handler;
				return  HANDLER_OK;
		}
}
led_handler_status_t os_queue_put_handler_1
                      (
                      void  *const queue_handler,
                      void  *const          item,
                      uint32_t            timeout   
                      )
{
#ifdef HANDLER_1_DEBUG
		printf("os_queue_put_handler_1\r\n");
#endif
    led_handler_status_t ret_handler=HANDLER_OK;
    BaseType_t ret=pdTRUE;
    if(NULL==queue_handler||
       NULL==item					||
       timeout >portMAX_DELAY)
		{
				return HANDLER_ERRORRESOURCE;
		}
    else
    {
      ret=xQueueSend(queue_handler,item,timeout);
      if(ret==pdFALSE)
      {
          ret_handler=HANDLER_ERROR;
      }
      return ret_handler;
    }
}



led_handler_status_t os_queue_get_handler_1
                      (
                      void  *const queue_handler,
                      void  *const          msg,
                      uint32_t            timeout   
                      )
{
#ifdef HANDLER_1_DEBUG
		printf("os_queue_get_handler_1\r\n");
#endif
    led_handler_status_t ret_handler=HANDLER_OK;
    BaseType_t ret=pdTRUE;
    if(NULL==queue_handler||
       NULL==msg					||
       timeout >portMAX_DELAY)
		{
				return HANDLER_ERRORRESOURCE;
		}
    else
    {
      ret=xQueueReceive(queue_handler,msg,timeout);
      if(ret==pdTRUE)
      {
          ret_handler=HANDLER_OK;
					return ret_handler;
      }
			else
			{
					ret_handler=HANDLER_ERROR;
					return ret_handler;
			}
    }
}

led_handler_status_t os_queue_delete_1
                      (
                      void  *const queue_handler   
                      )
{
#ifdef HANDLER_1_DEBUG
		printf("os_queue_delete_1\r\n");
#endif
     if(NULL==queue_handler)
		{
				return HANDLER_ERRORRESOURCE;
		}
    vQueueDelete(queue_handler);
    return HANDLER_OK;
}


handler_os_queue_t os_queue_1={
    .pf_os_queue_create=pf_os_queue_create_handelr_1,
    .pf_os_queue_put=os_queue_put_handler_1,
    .pf_os_queue_get=os_queue_get_handler_1,
    .pf_os_queue_delete=os_queue_delete_1,
};




//enter critial
led_handler_status_t handler1_os_critical_enter(void)
{
#ifdef HANDLER_1_DEBUG
		printf("handler1_os_critical_enter\r\n");
#endif
  vPortEnterCritical();
  return HANDLER_OK;
}
//exit critial
led_handler_status_t handler1_os_critical_exit(void)
{
#ifdef HANDLER_1_DEBUG
		printf("handler1_os_critical_exit\r\n");
#endif
  vPortExitCritical();
  return HANDLER_OK;
}

handler_os_critical_t handeler1_os_critical={
        .pf_os_critical_enter=handler1_os_critical_enter,
        .pf_os_critical_exit=handler1_os_critical_exit,
};

led_handler_status_t get_time_handler(uint32_t * const os_tick)
{
    if(NULL==os_tick)
    {
        return HANDLER_ERRORRESOURCE;
    }
		*os_tick=HAL_GetTick();
		return HANDLER_OK; 
}

handler_time_base_ms_t handler_time_base={
	.pf_get_time_ms=get_time_handler,
};


 led_handler_status_t handler_thread_create(
            void * const        task_code,    /*define internal*/
            const char *const   task_name,    /*define external*/
            const uint32_t      stack_depth,  /*define external*/
            void * const        parameters,   /*define internal*/
            uint32_t            priority,     /*define external*/
            void **const        task_handler)
{

      BaseType_t ret=pdTRUE;
#ifdef HANDLER_1_DEBUG
		printf("handler_thread_create\r\n");
#endif
    ret=xTaskCreate((TaskFunction_t)task_code,
                "led_handle_1",
                128*4,
                parameters,
                (osPriority_t) osPriorityNormal,
                (TaskHandle_t *)task_handler);
    if(pdTRUE!=ret)
    {
      return HANDLER_ERRORRESOURCE;
    }
    return HANDLER_OK;
}

led_handler_status_t handler_thread_delete(void *task_handler)
{
#ifdef HANDLER_1_DEBUG
		printf("handler_thread_delete\r\n");
#endif
		if(NULL==task_handler)
		{
				return HANDLER_ERRORRESOURCE;
		}
    vTaskDelete(task_handler);
    return HANDLER_OK;

}


handler_os_thread_t handler_os_thread={
  .pf_os_thread_create=handler_thread_create,
  .pf_os_thread_delete=handler_thread_delete
};

//*****************BSP Layer adapters****************************//
//******************************  Declaring **********************************//


/**
 *@brief init all the resources
 *
 * Steps:
 *1.mix up all the resources in the system
 *
 * 
 * 
 * @param[0]  bsp_led_driver_t      :Pointer to the bsp_led_driver_t instance 
 *                                  to be initialized.
 * @param[1]  os_delay_ms_t         :Pointer to the OS delay function, 
 *                                   only valid when OS_SUPPIRTING is defined
 * @param[2]  handler_os_queue_t    :Pointer to the OS queue, only valid when
 *                                   OS_SUPPIRTING is defined.
 * @param[3]  time_base_ms_t        :timebase Pointer to the time-base function.
 * 
 * @return led_handler_status_t :The status of runing   
  
**/

system_status_t system_init_resources(void);

#endif //#ifndef __SYSTEM_ADAPTION_H__